<template>
  <div>
    <el-container>
      <el-header height="70px">
        <p class="header_title">会员卡列表</p>
        <!--  -->
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>会员卡维护</el-breadcrumb-item>
          <el-breadcrumb-item>会员卡列表</el-breadcrumb-item>
          <el-breadcrumb-item></el-breadcrumb-item>
        </el-breadcrumb>
      </el-header>

      <el-main>
        <el-card class="box-card">
          <div slot="header">
            <span>会员卡列表</span>
            <span style="float: right;">
              <i class="el-icon-arrow-up"></i>
              <i class="el-icon-close"></i>
            </span>
          </div>

          <el-row :gutter="20">
            <el-col :span="4">
              <el-button type="primary" class="add" @click="addDialogVisible = true">添加</el-button>
            </el-col>
            <el-col :span="10">
                <b class="sum_data">数据总数：</b>
               <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pagination.pagenum"
                :page-size="pagination.pagesize"
                 :page-sizes="[3, 5, 10, 20]"
                layout="total, sizes"
                :total="pagination.total"
                class="top_nav">
               </el-pagination>
            </el-col>
          </el-row>

          <!-- 表格 -->
          <el-table :data="pagelist" style="width: 100%" border stripe>
            <el-table-column prop="duration" label="会员时长(日)">
            </el-table-column>
            <el-table-column prop="price" label="价格(￥)"> </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="showEditDialog(scope.row)">编辑</el-button>
                <el-button type="danger" size="mini" @click="removeBehavior(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-main>
    </el-container>

    <!-- 添加对话框 -->
    <el-dialog
      title="添加VIP卡信息"
      :visible.sync="addDialogVisible"
      width="60%"
      @close="addDialogClosed">
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
        class="demo-ruleForm"
      >
          <el-form-item label="会员时长" prop="id">
            <el-input
              v-model="addForm.duration"
              type="number"
              placeholder="输入时长"
            ></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="name">
            <el-input
              v-model=" addForm.price"
              type="price"
              placeholder="请输入价格"
            ></el-input>
          </el-form-item>
        </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑对话框 -->
     <el-dialog
      title="修改行为信息"
      :visible.sync="editDialogVisible"
      width="60%"
      @close="editDialogClosed">
          <el-form
        :model="editlist"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
      <el-form-item label="等级" prop="name">
          <el-input v-model="editlist.name" type="text" disabled></el-input>
        </el-form-item>
        <el-form-item label="时长" prop="duration">
          <el-input v-model="editlist.duration" ></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="editlist.price" type="text"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editActionInfo">确 定</el-button>
      </span>
     </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      cardInfo: {
        vips: "",
        total: "",
      },
      // 存放actionInfo里面的vips数据
      cardlist: [],
      pagination: {
        // 当前页数
        pagenum: 1,
        // 页面显示多少条数据
        pagesize: 5,
        // 总数
        total: 7,
      },
      pagelist: [],
      addDialogVisible: false,
      addForm: {
        id:'',
        name:'',
        price:'',
        duration:''
      },
      // 添加表单的校验
      addFormRules: {
        duration: [{ required: true,message:"请输入数值", trigger: "blur" }],
        price: [{ required: true,message:"请输入数值",trigger: "blur" }],
      },
      // 编辑对话框的显示与隐藏
       editDialogVisible: false,
      // 查询用户信息对象
       editForm: {
        duration: "",
        price: "",
      },
       editFormRules: {
        duration: [{ required: true, message: "请输入数值", trigger: "blur" }],
       price: [{ required: true, message: "请输入价格", trigger: "blur" }],
      },
      editlist: [],

    };
  },
  methods: {
    async getCardList() {
      const res = await this.http.get("/vip/select", this.pagination);

      this.pagelist = res.vips;
      this.pagination.total = res.total;
      console.log(res);
    },
    // 监听pagesize的改变
    handleSizeChange(newSize) {
      // console.log(newSize);
      this.pagination.pagesize = newSize;
      this.getCardList();
    },
    // 监听 页码值 改变
    handleCurrentChange(newPage) {
      // console.log(newPage);
      this.pagination.pagenum = newPage;
      this.getCardList();
    },
     addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    // 添加
    onSubmit(){
         this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        // 发起添加网络请求
        const { res } = await this.http.post("/vip/add", this.addForm);

        if (status !== 400) {
          this.$message.error("添加行为失败");
        }
        this.$message.success("添加行为成功");
        this.addDialogVisible = false;
          this.getCardList();
         });
    },

     //编辑行为
    async showEditDialog(row) {
      const res = await this.http.post("/vip/update", this.editForm);

      this.editlist = row;
      this.editDialogVisible = true;
    },
    // 监听编辑对话框关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
     // 是否完成编辑行为信息
    editActionInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        console.log(valid);
        if (!valid) return;
        // 发起修改信息的信息的数据请求
        const res = await this.http.post("/vip/update", this.editlist, {
          duration: this.editlist.duration,
          price: this.editlist.price,
        });

        if (status !== 400) {
          this.$message.error("编辑行为信息失败");
        }
        console.log(res);
        // 关闭对话框
        this.editDialogVisible = false;
        // 刷新数据列表
        this.getCardList();
        // 提交修改成功
        this.$message.success("更新行为信息成功");
      });
    },
    // 删除
    
    async removeBehavior(id){
      const res = await this.http.post('/vip/delete',{id:id})

      // console.log("----row-----");
      // console.log(row);
      // console.log("---deletelist------");
      // console.log(this.deletelist);
      // console.log("----res-----");
      // console.log(res);
       this.getCardList();
    }
  },
  created() {
    this.getCardList();
  },
};
</script>

<style scoped></style>
